package cn.butcher21.controller;

import cn.butcher21.dao.DaoWork;
import cn.butcher21.dao.DaoWorkImpl.DaoWorkImpl;
import cn.butcher21.pojo.Work;
import cn.butcher21.utils.ToLayuiTableData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * butcher create in 2021/04/10/16:44
 * <p>
 * 获取任务表信息
 */
@WebServlet("/getMsg")
public class GetMsgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        Cookie[] cookies = request.getCookies();
//        String projectNum = "1";
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if (cookie.getName().equals("project")) {
//                    projectNum = cookie.getValue();
//                    break;
//                }
//            }
//        }

        String pNum = request.getParameter("pNum");
        String gid = (String) request.getSession().getAttribute("userid");
        DaoWork daoWork = new DaoWorkImpl();
        try {
            List<Work> msg = daoWork.getMsg(pNum, gid);
            String format = ToLayuiTableData.format(msg);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print(format);
            writer.flush();
            writer.close();
        } catch (SQLException e) {
            e.printStackTrace();
            response.setStatus(500);
        }
    }
}
